package com.example.constellation.home;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.constellation.MainActivity;
import com.example.constellation.R;

public class WelcomeActivity extends AppCompatActivity {
    TextView tv;//显示倒计时
    int count=5;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what==1){
                count--;
                if(count==0){
                    //判断是否是第一次进入App
                    boolean isfirst=first_run.getBoolean("isFirst",true);//第二个参数代表默认值为true
                    Intent intent;
                    if(isfirst){
                        intent=new Intent(WelcomeActivity.this, GuideActivity.class);
                        SharedPreferences.Editor editor=first_run.edit();
                        editor.putBoolean("isFirst",false);
                        editor.commit();
                    }else {
                        intent=new Intent(WelcomeActivity.this, MainActivity.class);
                    }
                    startActivity(intent);
                    finish();
                }else{
                    tv.setText(String.valueOf(count));
                    handler.sendEmptyMessageDelayed(1,1000);
                }
            }
        }
    };
    private SharedPreferences first_run;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        tv=findViewById(R.id.welcome_tv);
        first_run = getSharedPreferences("first_run", MODE_PRIVATE);
        handler.sendEmptyMessageDelayed(1,1000);
    }
}
